﻿using CSharpDay15__Auth_.Data.Model;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations; //Used for AddOrUpdate();
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpDay15__Auth_.Data
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db, bool roles = false, bool users = false, bool todos = false)
        {
            if (roles) SeedRoles(db);
            if (users) SeedUsers(db);
            if (todos) SeedToDos(db);
        }

        private static void SeedRoles(ApplicationDbContext db)
        {
            var store = new RoleStore<IdentityRole>(db);
            var manager = new RoleManager<IdentityRole>(store);

            //if there is no Role in the database called "User"... create one
            if (!manager.RoleExists(Roles.User))
            {
                manager.Create(new IdentityRole() { Name = Roles.User });
            }

            if (!manager.RoleExists(Roles.Admin))
            {
                manager.Create(new IdentityRole() { Name = Roles.Admin });
            }
        }

        private static void SeedUsers(ApplicationDbContext db)
        {
            var store = new UserStore<ApplicationUser>(db);
            var manager = new UserManager<ApplicationUser>(store);

            //if no users exists in the database that have a username of "test@test.com"... create one
            //Out of the Box MVC.... USERNAME HAVE TO BE THE SAME AS THE EMAIL ADDRESS
            if (!db.Users.Any(x => x.UserName == "test@test.com"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    Email = "test@test.com",
                    UserName = "test@test.com",
                    IsDisabled = false,
                    EmailConfirmed = true,
                    PhoneNumber = "555-555-5555",
                    PhoneNumberConfirmed = true,
                    TwoFactorEnabled = false
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, Roles.User);
            }

            //Admin User
            if (!db.Users.Any(x => x.UserName == "admin@test.com"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    Email = "admin@test.com",
                    UserName = "admin@test.com",
                    IsDisabled = false
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, Roles.Admin);
            }
        }

        private static void SeedToDos(ApplicationDbContext db)
        {
            string Test = db.Users.FirstOrDefault(x => x.Email == "test@test.com").Id;
            string Admin = db.Users.FirstOrDefault(x => x.Email == "admin@test.com").Id;

            db.ToDoes.AddOrUpdate(x => x.Id,
                new ToDo() { Id = 1, UserId = Test, DateCreated = DateTime.Now, Description = "Learn MVC", DateCompleted = null },
                new ToDo() { Id = 2, UserId = Test, DateCreated = DateTime.Now, Description = "Learn Angular", DateCompleted = DateTime.Now.AddDays(1) },
                new ToDo() { Id = 3, UserId = Test, DateCreated = DateTime.Now, Description = "Learn C#", DateCompleted = null },
                new ToDo() { Id = 4, UserId = Admin, DateCreated = DateTime.Now, Description = "Learn JavaScript", DateCompleted = DateTime.Now.AddHours(2) },
                new ToDo() { Id = 5, UserId = Admin, DateCreated = DateTime.Now, Description = "Learn SQL", DateCompleted = null },
                new ToDo() { Id = 6, UserId = Admin, DateCreated = DateTime.Now, Description = "Learn APIs", DateCompleted = DateTime.Now.AddDays(4) }
                );
        }
    }
}